iT邦幫忙

2021 iThome 鐵人賽

DAY 24
0
Mobile Development

企劃實現系列 第 24

企劃實現(24)

  • 分享至 

  • xImage
  •  

使用tablatout串fragment
xml程式碼

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="fitXY"
        android:paddingBottom="86dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tablayoutFragment"
        android:layout_width="match_parent"
        android:layout_height="86dp"
        android:background="#EFE5B4"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

        <com.google.android.material.tabs.TabItem
            android:id="@+id/main"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:icon="@drawable/main"

            />
        <com.google.android.material.tabs.TabItem
            android:id="@+id/discuss"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:icon="@drawable/main"

            />

        <com.google.android.material.tabs.TabItem
            android:id="@+id/calender"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:icon="@drawable/calender" />

        <com.google.android.material.tabs.TabItem
            android:id="@+id/message"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:icon="@drawable/message" />

        <com.google.android.material.tabs.TabItem
            android:id="@+id/personal"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:icon="@drawable/personal" />

    </com.google.android.material.tabs.TabLayout>


</androidx.constraintlayout.widget.ConstraintLayout>

activity程式碼

package com.example.graduationtopic;



import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.example.graduationtopic.personal.Personal_Fragment;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    int num=5;

    ViewPager viewPager;
    TabLayout tabLayout;
    List<Fragment> fragmentlist=new ArrayList<Fragment>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tabLayout=findViewById(R.id.tablayoutFragment);
        tabLayout.setTabMode(TabLayout.MODE_AUTO);
        viewPager=findViewById(R.id.view_pager);

        fragmentlist.add(new MainFragment());
        fragmentlist.add(new Discuss_Fragment());
        fragmentlist.add(new Calendar_Fragment());
        fragmentlist.add(new MessageFragment());
        fragmentlist.add(new Personal_Fragment());

        MyAdapter fragmentAdater =new MyAdapter(getSupportFragmentManager());
        viewPager.setAdapter(fragmentAdater);
        initView();



    }
    private void initView(){
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }



    //    tabLayout.setupWithViewPager(viewPager);
//    }
  public class MyAdapter extends FragmentPagerAdapter {
        public MyAdapter(FragmentManager fm){
            super(fm);
        }

      @Override
      public int getCount() {
          return num;
      }

      @NonNull
      @Override
      public Fragment getItem(int position) {
          return fragmentlist.get(position);
      }
  }



}

上一篇
企劃實現(23)
下一篇
企劃實現(25)
系列文
企劃實現30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言